package com.mew.user.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mew.common.dc.db.LevelConfigService;
import com.mew.common.dc.db.ProfileService;
import com.mew.common.dc.db.UserService;
import com.mew.common.dc.mapper.ProfileMapper;
import com.mew.common.dc.mapper.UserMapper;
import com.mew.common.dc.redis.GameRoomRedisService;
import com.mew.common.enm.PropType;
import com.mew.common.entity.config.Game;
import com.mew.common.entity.config.LevelVip;
import com.mew.common.entity.user.Profile;
import com.mew.common.entity.user.User;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.user.SimpleUserBO;
import com.mew.common.rest.user.UserDetailedInfoResponseMessage;
import com.mew.common.rest.user.VipInfoMessage;
import com.mew.prop.service.PropService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author dylin
 * @create 2021/1/27 0027
 */
@Service
public class UserInfoService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private ProfileService profileService;
    @Autowired
    private ProfileMapper profileMapper;
    @Autowired
    private GameRoomRedisService gameRoomRedisService;
    @Autowired
    private LevelConfigService levelConfigService;
    @Autowired
    private FriendService friendService;
    @Autowired
    private PropService propService;


    /**
     * 更新用户信息
     * @param user
     * @return
     */
    public void updateByID(User user){
        userMapper.updateById(user);
        //更新redis
        userService.getUserById(user.getId());
    }

    public int updateNickname(User user) {
        long propNumber = propService.getPropNumber(user.getId(), PropType.EditNameCard.code());
        if(propNumber == 0){
            return ResponseCode.notChangeName.code();
        }
        updateByID(user);
        propService.consume(user.getId(), PropType.EditNameCard.code(),1);
        return ResponseCode.Success.code();
    }
	/**
	 * 更新用户账号
	 */
	public int updateAccount(User user){
		User u = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getAccount, user.getAccount()));
		if(u != null){
			return ResponseCode.AccountIsUse.code();
		}
        updateByID(user);
        return ResponseCode.Success.code();
	}

    public void updateProfileByid(Profile profile){
        profileMapper.updateById(profile);
        //更新redis
        profileService.getProfileById(profile.getUserId());
    }

    public int bindTelephone(long uid,String telephone){
        long us= userMapper.selectCount(new LambdaQueryWrapper<User>().eq(User::getTelNumber, telephone));
        if(us>0){
            return ResponseCode.UserTelRepeat.code();
        }
        User dbUser=User.builder().id(uid).telNumber(telephone).build();
        updateByID(dbUser);
        return ResponseCode.Success.code();
    }

    public UserDetailedInfoResponseMessage getDetailedInfo(long uid,long qid){
        User user=userService.getRedisUserById(qid);
        Profile profile = profileService.getRedisProfileById(qid);
        if(user==null||profile==null){
            return null;
        }
        return UserDetailedInfoResponseMessage.builder()
                .id(user.getId())
                .gid(user.getGid())
                .vipLevel(profile.getVipLevel())
                .headPath(user.getHeadPath())
                .nickname(user.getNickname())
                .goldNumber(profile.getGoldNumber())
                .paymentTotal(profile.getPaymentTotal())
                .signature(user.getSignature())
                .isFriend(friendService.isFriend(uid,qid)?1:0)
                .build();
    }

    public UserDetailedInfoResponseMessage getDetailedInfo(long uid,String gid){
        User user=userService.getUserByGid(gid);
        if(user==null){
            return null;
        }
        Profile profile = profileService.getRedisProfileById(user.getId());
        if(user==null){
            return null;
        }
        return UserDetailedInfoResponseMessage.builder()
                .id(user.getId())
                .gid(user.getGid())
                .vipLevel(profile.getVipLevel())
                .headPath(user.getHeadPath())
                .nickname(user.getNickname())
                .goldNumber(profile.getGoldNumber())
                .paymentTotal(profile.getPaymentTotal())
                .signature(user.getSignature())
                .isFriend(friendService.isFriend(uid,user.getId())?1:0)
                .build();
    }

    /**
     * 单人简单信息  添加好友广播用
     * @param uid
     * @return
     */
    public SimpleUserBO getSimpleInfo(long uid){
        User user=userService.getRedisUserById(uid);
        if(user==null){
            return null;
        }
        Profile profile = profileService.getRedisProfileById(uid);
        if(user==null){
            return null;
        }
        SimpleUserBO su=new SimpleUserBO();
        su.setUid(user.getId());
        su.setGid(user.getGid());
        su.setNickname(user.getNickname());
        su.setHeadPath(user.getHeadPath());
        su.setGoldNumber(profile.getGoldNumber());
        su.setVipLevel(profile.getVipLevel());
        su.setRes(1);
        return su;
    }

    public VipInfoMessage vip(long uid) {

        Profile profile = profileService.getRedisProfileById(uid);

        VipInfoMessage message = new VipInfoMessage();
        message.rate = LevelConfigService.rate;
        message.exp = profile.getVipExpValue();
        message.vip = profile.getVipLevel();
        ArrayList<VipInfoMessage.Level> levels = new ArrayList<>();
        message.levels = levels;

        List<LevelVip> levelVips = levelConfigService.getAllLevelVipConfig();
        Map<Integer, List<Game>> map = gameRoomRedisService.findAllGame().stream().collect(Collectors.groupingBy(Game::getUnlockLv));
        for (LevelVip levelVip : levelVips) {
            VipInfoMessage.Level level = new VipInfoMessage.Level();
            level.toLeve(levelVip);
            levels.add(level);
        }

        return message;
    }

    public void guide(long uid, int guideType) throws Exception {
        User user = userService.getRedisUserById(uid);
        if (null == user){
            throw new Exception("User is null uid: "+uid);
        }
        user.setGuideType(guideType);
        User other = new User();
        other.setId(uid);
        other.setGuideType(guideType);
        userService.updateUser(user, other);
    }
}
